package com.dyz.leetcode;

public class SearchRange34 {
    public int[] searchRange(int[] nums, int target) {
        int [] res = {-1,-1};
        int n = nums.length;
        if(n==0){return res;}
        int left = 0;
        int right = n-1;
        while (left<=right){
            int mid = left +(right-left)/2;
            if(nums[mid]==target){
                //找到目标
                //两边开始寻找
                left = mid;
                right = mid;
                while (left>0 && nums[left]==nums[left-1]){left--;}
                while (right < n - 1 && nums[right] == nums[right+1]) {right++;}
                res[0] = left;
                res[1] = right;
                return res;
            }else if(nums[mid]>target){right = mid-1;}
            else {left=mid+1;}
        }
        return res;
    }
}
